-- NewTeamMemberHeadRender 对 BaseTeamMemberHead.csb 的解析
-- 队伍成员头像render

local AvatarIconNode = require("app.widget.AvatarIconNode")

local PartnerHeadIconRender = app.mvc.createRenderClass(...)

-- UserBaseInfo like
function PartnerHeadIconRender.create(node)
	local sprite = PartnerHeadIconRender.extend(node or cc.CSLoader:createNode(Res.PartnerHeadIcon))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end


function PartnerHeadIconRender:init(data)
	self.super.init(self, data)
	self._tipsEnabled = true
	display.uiAddClick(self._ui.Panel, function(sender)
		if self._tipsEnabled then
			self:showTips(sender)
		end
	end)
	self:refresh()
end


function PartnerHeadIconRender:setTouchEnabled(trueOrFalse)
	self._ui.Panel:setTouchEnabled(trueOrFalse)
end

function PartnerHeadIconRender:setTipsEnabled(trueOrFalse)
	self._tipsEnabled = trueOrFalse
end


function PartnerHeadIconRender:showTips(sender)
	local data = self._data
	if not data then
		return
	end
	local params = {}
	params.tipsType = Enums.TipsType.partner
	params.dataTemp = data
	params.unShowOrigin = data:isHave()
	params.contents = self:_getTipContents(self._data)

	local otherParams = {}
	local size = sender:getContentSize()
	local pos = sender:convertToWorldSpace(cc.p(0,0))
	otherParams.tipPos = cc.p(pos.x - size.width/2, pos.y + size.height)

	EM:notify(ViewEvent.Partner_ShowTips, params, otherParams)
end


function PartnerHeadIconRender:getPanelSize()
	return self._ui.Panel:getContentSize()
end

function PartnerHeadIconRender:refresh()
    if not self._data then
        return
    end
    local data = self._data
    local quality = data:getQuality()
    self._ui["Panel/bg"]:loadTexture(Res.Quality_Partner_Icon[quality])
    self._ui["Panel/icon"]:loadTexture(data:getIcon())
	-- self._ui["Panel/class_icon"]:loadTexture(GD:getClassIcon( data:getClassId(), 2 ))
	self._ui["Panel/class_icon"]:setVisible(false)
	self._ui["Panel/Panel_lv/num"]:setString(data:getLevel())
	self._ui["Panel/star"]:loadTexture(string.format(Res.PartnerStarPathFormat, data:getStar()), 1)
	if data:isHave() then
		display.applyShaderByName(self._ui["Panel"])
	else
		display.applyShaderByName(self._ui["Panel"], "ShaderGray")
	end
end

function PartnerHeadIconRender:_getTipContents(partner)
	local contents = {}
	local part1 = {}
	local q = partner:getQuality()

	local colorNormal = cc.c3b(255, 225, 132)
	local colorGreen = cc.c3b(0, 220, 220)
	local colorGray = cc.c3b(160, 160, 160)

	table.insert(part1, {text = L("PartnerQualityString1"), color = colorNormal})
	local props = partner:getShowPlayerProps()
	for k, v in pairs(props) do
		local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
		table.insert(part1, {text = string.format(L("%s%s"), name or "", value or ""), color = colorGreen})
	end

	table.insert(contents, part1)
	local part2 = {}


    local nextProps = partner:getShowPlayerProps(q+1)
    if #nextProps > 0 then
		local qualityName = partner:getQualityText()
        table.insert(part2, {text = string.format(L("PartnerQualityString2"),qualityName), color = colorNormal})
        for k, v in pairs(nextProps) do
            local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
            table.insert(part2, {text = string.format(L("%s%s"), name or "", value or ""), color = colorGray})
        end
	end

	if #part2 > 0 then
		table.insert(contents, part2)
	end
	return contents
end

return PartnerHeadIconRender
